package com.czlw.module_activity.viewmodel

import androidx.databinding.ObservableField
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.CommunityBean
import com.czl.base.data.bean.NoticeListBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.util.RxThreadHelper

class BulletinListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model){

    var currentPage = 0
    val uc = UiChangeEvent()
    val notice=ObservableField<NoticeListBean.Record>()
    var showTitle=ObservableField<Boolean>(true)
    var showRv=ObservableField<Boolean>(true)
    class UiChangeEvent {
        val refreshListEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val loadCompleteEvent: SingleLiveEvent<NoticeListBean> = SingleLiveEvent()
        val toBulletinDetailEvent: SingleLiveEvent<Int> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getBullletin()
    })
    val onFirstBulletinClick: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.toBulletinDetailEvent.postValue(notice.get()!!.id)
    })

    private fun getBullletin() {

        model.getNoticeList(mapOf(
            "areaNo" to model.getAreaId(),
            "pageNum" to (currentPage+1).toString(),
            "pageSize" to "10",
        ))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<NoticeListBean>>(){
                override fun onResult(t: BaseBean<NoticeListBean>) {
                    if (t.code == 200) {
                        currentPage++
//                        var nbs= mutableListOf<NoticeResponseBean.Record>()
//                        for (i in 1..30){
//                            nbs.add(NoticeResponseBean.Record("2022-01-24 16:59:07",i,0, "停电$i",2))
//                        }
//                        var nb=NoticeResponseBean("",1,100,true, listOf(),1, nbs,true,30,30)
                        if (t.data!!.current==1){
                            notice.set(null)
                            if (t.data!!.records.isNotEmpty()&& t.data?.records?.size!! > 0){
                                notice.set((t.data!!.records as MutableList<*>).removeAt(0) as NoticeListBean.Record?)
                            }
                        }
                        if (t.data!!.records.size==0){
                            showTitle.set(false)
                        }else{
                            showTitle.set(true)
                        }
                        if (t.data!!.records.size==0&&notice.get()!=null){
                            showRv.set(false)
                        }else{
                            showRv.set(true)
                        }
//                        if (t.data?.records?.size!! > 0){
                        uc.loadCompleteEvent.postValue(t.data)
//                        }else{
//                            uc.loadCompleteEvent.postValue(null)
//                        }
//                        uc.loadCompleteEvent.postValue(nb)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }


    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 0
        getBullletin()
    })
}